package com.boot2.core.dao.mongo;

import com.boot2.core.dao.AbstractRestrictions;
import com.boot2.core.utils.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * @author zhangweilin
 * @description: MongoRestrictions条件构造器 用于创建条件表达式
 * @date 2017年12月27日 上午11:14:03
 */
public class MongoRestrictions extends AbstractRestrictions {

    /**
     * 等于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria eq(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).is(value);
    }

    /**
     * 不等于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria ne(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).ne(value);
    }

    /**
     * 模糊匹配查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria like(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        //mongo中没有like查询，只有正则方式
        return new Criteria(fieldName).regex(value.toString());
    }

    /**
     * 大于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria gt(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).gt(value);
    }

    /**
     * 大于等于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria gte(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).gte(value);
    }

    /**
     * 小于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria lt(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).lt(value);
    }

    /**
     * 小于等于查询
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static Criteria lte(String fieldName, Object value) {
        if (StringUtils.isEmpty(fieldName) || null == value) {
            return null;
        }
        return new Criteria(fieldName).lte(value);
    }

    /**
     * 范围查询
     *
     * @param fieldName
     * @param valueArr
     * @return
     */
    public static Criteria between(String fieldName, Object[] valueArr) {
        if (StringUtils.isEmpty(fieldName) || null == valueArr || valueArr.length < 2) {
            return null;
        }
        if (valueArr[0] == null || valueArr[1] == null) {
            return null;
        }
        //monogo没有专门的 between查询
        return new Criteria(fieldName).gte(valueArr[0]).andOperator(new Criteria(fieldName).lte(valueArr[1]));
    }

    /**
     * and查询
     *
     * @param criteria1
     * @param criteria2
     * @return
     */
    public static Criteria and(Criteria criteria1, Criteria... criteria2) {
        if (null == criteria1 || null == criteria2) {
            return null;
        }
        return criteria1.andOperator(criteria2);
    }

    /**
     * or查询
     *
     * @param criteria1
     * @param criteria2
     * @return
     */
    public static Criteria or(Criteria criteria1, Criteria... criteria2) {
        if (null == criteria1 || criteria2 == null) {
            return null;
        }
        return criteria1.orOperator(criteria2);
    }

    /**
     * in查询
     *
     * @param fieldName
     * @param objArr
     * @return
     */
    public static Criteria in(String fieldName, Object... objArr) {
        if (StringUtils.isEmpty(fieldName) || objArr == null) {
            return null;
        }
        return new Criteria(fieldName).in(objArr);
    }

    /**
     * in查询
     *
     * @param fieldName
     * @param collections
     * @return
     */
    public static Criteria in(String fieldName, Collection collections) {
        if (StringUtils.isEmpty(fieldName) || CollectionUtils.isEmpty(collections)) {
            return null;
        }
        return new Criteria(fieldName).in(collections);
    }

    /**
     * not in查询
     *
     * @param fieldName
     * @param objArr
     * @return
     */
    public static Criteria nin(String fieldName, Object... objArr) {
        if (StringUtils.isEmpty(fieldName) || objArr == null) {
            return null;
        }
        return new Criteria(fieldName).nin(objArr);
    }

    /**
     * not in查询
     *
     * @param fieldName
     * @param collections
     * @return
     */
    public static Criteria nin(String fieldName, Collection collections) {
        if (StringUtils.isEmpty(fieldName) || CollectionUtils.isEmpty(collections)) {
            return null;
        }
        return new Criteria(fieldName).nin(collections);
    }
}